package edu.utils;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.stereotype.Component;

/**
 * 短信验证码配置，对接腾讯云短信服务
 */

public final class SMSUtil {

    private final static String APP_ID = "1400237523";
    private final static String SECRET_ID = "AKIDFKFzVvxbtxoRZbkEgvEMHoBKH2bxH3GN";
    private final static String SECRET_KEY = "FOIzYu3YrumbhZBKCtQkye5IVUL8j5FF";
    private final static String TEMPLATE_ID = "382445";
    private final static String SMS_SIGN = "GTAOnlinee";
    private final static Credential SENDER_CONFIG = new Credential(SECRET_ID,SECRET_KEY);
    private final static SmsClient SENDER = new SmsClient(SENDER_CONFIG,"");
    private static final Log log = LogFactory.get("短信接口工具类");

    public static SendStatus sendVerificationCode(String phone,String verificationCode) {
        //创建请求
        SendSmsRequest request = buildRequest(phone, verificationCode);
        //发送请求并获取结果
        SendStatus result = null;
        try {
            //发送验证码
            result = SENDER.SendSms(request).getSendStatusSet()[0];
        }catch (TencentCloudSDKException e) {
            log.error("sendVerificationCode => 请求失败");
            return null;
        }
        log.info("sendVerificationCode => code: " + result.getCode() + ",errorMsg: " + result.getMessage());
        return result;
    }

    private static SendSmsRequest buildRequest(String phone,String verificationCode) {
        SendSmsRequest request = new SendSmsRequest();
        request.setSmsSdkAppid(APP_ID);
        request.setSign(SMS_SIGN);
        request.setTemplateID(TEMPLATE_ID);
        request.setPhoneNumberSet(new String[]{"86" + phone});
        request.setTemplateParamSet(new String[]{"",verificationCode});
        return request;
    }


    public static String createRandomVerificationCode() {
        return String.valueOf((int)((Math.random() * 9 + 1) * 100000));
    }



}
